package com.hoodbrains.insideloc.maps;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.Point;

public class Gondole extends DrawableObject{

	/** Coordinate of the upper left corner centimeters */
	private int x;
	/** Coordinate of the upper left corner centimeters */
	private int y;
	/** width	-->		x */
	private int width;

	/** height	-->		y */
	private int height;
	private boolean isSelected ;
	private boolean isVertical;
	
	private List<Rayon> cote1;
	private List<Rayon> cote2;
	
	public Gondole(){
		super();
		cote1 = new ArrayList<Rayon>();
		cote2 = new ArrayList<Rayon>();
	}
	
	public void configurePaint(){
		if (isSelected)
		{
			configurePaintForSelected();
		}
		else
		{
			this.paint.setStrokeWidth(1.0f);
			this.paint.setStyle(Style.STROKE);
			this.paint.setColor(Color.BLACK);
		}
	
	}
	
	
	public void draw(Canvas canvas,Map<String,Point> rayons) {
		super.draw(canvas);
		int stepx = (isVertical)?0:1;
		int stepy = (isVertical)?1:0;
		
		int rectTopX = this.x+(this.width/2)*stepy ;
		int rectTopY = this.y+(this.height/2)*stepx ;
		int rectBotX = this.x+this.width*stepx + (this.width/2)*stepy ;
		int rectBotY = this.y+(this.height/2)*stepx + this.height*stepy;
		//Draw cote 1 : 
		if(this.cote1.size()>0){
			rectTopX = this.x;
			rectTopY = this.y;
			int locX1 = this.x;
			int locY1 = this.y;
			int rwidth1  = (this.isVertical)?this.width/2:(this.width/this.cote1.size());
			int rheight1 = (this.isVertical)?(this.height/this.cote1.size()):this.height/2;
			int diff1  = (this.isVertical)?this.height-rheight1*this.cote1.size():this.width-rwidth1*this.cote1.size();
			int lastindex1 = this.cote1.size()-1;
			for (int i = 0 ; i < lastindex1 ; i ++){
				this.cote1.get(i).draw(canvas,locX1+i*rwidth1*stepx,locY1+i*rheight1*stepy,rwidth1,rheight1);
				rayons.put(this.cote1.get(i).getType(),new Point(locX1+i*rwidth1*stepx+rwidth1/2,locY1+i*rheight1*stepy+rheight1/2));
			}
			this.cote1.get(lastindex1).draw(canvas,
					locX1+lastindex1*rwidth1*stepx,
					locY1+lastindex1*rheight1*stepy,
					rwidth1+stepx*diff1,
					rheight1+stepy*diff1);
			rayons.put(this.cote1.get(lastindex1).getType(),
					new Point(locX1+lastindex1*rwidth1*stepx+(rwidth1+stepx*diff1)/2,
							  locY1+lastindex1*rheight1*stepy+(rheight1+stepy*diff1)/2));
		}
		//Draw cote 2 :		
		if (this.cote2.size()>0){
			rectBotX = this.x + this.width;
			rectBotY = this.y + this.height;
			int locX2 = (this.isVertical)?this.x + this.width/2: this.x ;
			int locY2 = (this.isVertical)?this.y: this.y + this.height/2;
			int rwidth2  = (this.isVertical)?this.width/2:(this.width/this.cote2.size());
			int rheight2 = (this.isVertical)?(this.height/this.cote2.size()):this.height/2;
			int diff2  = (this.isVertical)?this.height-rheight2*this.cote2.size():this.width-rwidth2*this.cote2.size();
			int lastindex2 = this.cote2.size()-1;
			for (int i = 0 ; i < lastindex2 ; i ++){
				this.cote2.get(i).draw(canvas,locX2+i*rwidth2*stepx,locY2+i*rheight2*stepy,rwidth2,rheight2);
				rayons.put(this.cote2.get(i).getType(),new Point(locX2+i*rwidth2*stepx+rwidth2/2,locY2+i*rheight2*stepy+rheight2/2));
				//DrawingTools.drawLine(canvas, paint,new Point(locX2+i*rwidth2*stepx,locY2+i*rheight2*stepy),new Point(locX2+i*rwidth2*stepx+rwidth2,locY2+i*rheight2*stepy+rheight2));
			}
			this.cote2.get(lastindex2).draw(canvas,
					locX2+lastindex2*rwidth2*stepx,
					locY2+lastindex2*rheight2*stepy,
					rwidth2+stepx*diff2,
					rheight2+stepy*diff2);
			rayons.put(this.cote2.get(lastindex2).getType(),
					new Point(locX2+lastindex2*rwidth2*stepx+(rwidth2+stepx*diff2)/2,
							  locY2+lastindex2*rheight2*stepy+(rheight2+stepy*diff2)/2));
		}
		
		DrawingTools.drawRectangle(canvas, paint,new Point(rectTopX,rectTopY),new Point(rectBotX,rectBotY));
		DrawingTools.drawLine(canvas, paint,
				new Point(this.x+(this.width/2)*stepy,this.y+(this.height/2)*stepx),
				new Point(this.x+this.width*stepx + (this.width/2)*stepy,this.y+(this.height/2)*stepx + this.height*stepy));
	}
	
	
	
	private static List<Rayon> deserializeCoteElem(Element coteElem) {
		List<Rayon>cote = new ArrayList<Rayon>();
		NodeList rayonElems = coteElem.getElementsByTagName("rayon");
		for (int i=0; i<rayonElems.getLength(); i++) {
			Log.e("Gondole", "n°"+i);
			cote.add(Rayon.deserialize((Element)rayonElems.item(i)));
		}
		
		return cote;
	}
	
	public String toString() {
		return "Gondole: x: " + x +
				", y: " + y +
				", w: " + width +
				", h: " + height;
				
	}
	
	
	
	public static Gondole deserialize(Element elem) {

		Gondole gondole = new Gondole();
		
		gondole.x = Integer.parseInt(elem.getAttribute("x"));
		gondole.y = Integer.parseInt(elem.getAttribute("y"));
		gondole.width = Integer.parseInt(elem.getAttribute("largeur"));
		gondole.height = Integer.parseInt(elem.getAttribute("hauteur"));
		gondole.isVertical = "verticale".equals(elem.getAttribute("orientation"));
		
		Node cote1Elem = elem.getElementsByTagName("cote1").item(0);
		gondole.cote1 = deserializeCoteElem((Element)cote1Elem);
		Node cote2Elem = elem.getElementsByTagName("cote2").item(0);
		gondole.cote2 = deserializeCoteElem((Element)cote2Elem);
		
		Log.e("Gondole", gondole.toString());
		return gondole;
	}
	
	
	public void setVertical(boolean isVertical) {
		this.isVertical = isVertical;
	}

	private void serializeCote(Document doc, Element coteElem, List<Rayon> cote) {
		for (Rayon rayon : cote) {
			coteElem.appendChild(rayon.serialize(doc));
		}
		
	}
	
	public Element serialize(Document doc) {
		Element elem = doc.createElement("gondole");
		elem.setAttribute("x", String.valueOf(x));
		elem.setAttribute("y", String.valueOf(y));
		elem.setAttribute("largeur", String.valueOf(width));
		elem.setAttribute("hauteur", String.valueOf(height));
		elem.setAttribute("orientation", isVertical ? "verticale":"horizontale");
		Element cote1Elem = doc.createElement("cote1");
		serializeCote(doc, cote1Elem, cote1);
		elem.appendChild(cote1Elem);
		Element cote2Elem = doc.createElement("cote2");
		serializeCote(doc, cote2Elem, cote2);
		elem.appendChild(cote2Elem);
		
		return elem;
	}
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public List<Rayon> getCote1() {
		return cote1;
	}

	public List<Rayon> getCote2() {
		return cote2;
	}
	
	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
		
		
	}

	public boolean isSelected() {
		return isSelected;
	}
	private void configurePaintForSelected(){
		this.paint.setStrokeWidth(5.0f);
		this.paint.setStyle(Style.STROKE);
		this.paint.setColor(android.graphics.Color.RED);
	}
}
